//
//  GradientButton.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/25.
//

import UIKit

/// 渐变色背景的button
public class GradientButton: UIButton {
    @IBInspectable public var topColor: UIColor?
    @IBInspectable public var bottomColor: UIColor?
    @IBInspectable public var leftColor: UIColor?
    @IBInspectable public var rightColor: UIColor?
    @IBInspectable var borderWidth: CGFloat = 0
    @IBInspectable var cornerRadii: CGSize = .zero
    private var corners: UIRectCorner = .allCorners
    private var border: BorderType = .none
    
    /// 指定位置切圆角，在awakeFromNib或initFrame中调用
    ///
    /// - Parameters:
    ///   - corners: 需要切的角
    ///   - cornerRadii: 圆角半径
    ///   - border: 圆角边框类型 BorderType，默认不显示边框
    public func setCorner(corners: UIRectCorner, cornerRadii: CGSize, border: BorderType = .none) {
        self.corners = corners
        self.cornerRadii = cornerRadii
        self.border = border
    }

    public override func draw(_ rect: CGRect) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = rect
        
        gradientLayer.startPoint = CGPoint.zero
        gradientLayer.zPosition = -1
        if let topColor = topColor, let bottomColor = bottomColor {
            gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
            gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        }
        if let leftColor = leftColor, let rightColor = rightColor {
            gradientLayer.colors = [leftColor.cgColor, rightColor.cgColor]
            gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        }
        
        layer.insertSublayer(gradientLayer, at: 0)
        self.clipsToBounds = true // 确保按钮边界内的子图层能够被剪切
        
        var type: BorderType = .none
        if let borderColor = borderColor, borderWidth != 0 {
            type = .solid(width: borderWidth, color: borderColor)
        }
        bezierCorner(corners: corners, cornerRadii: cornerRadii, border: type)
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.frame
        
        gradientLayer.startPoint = CGPoint.zero
        gradientLayer.zPosition = -1
        if let topColor = topColor, let bottomColor = bottomColor {
            gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
            gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        }
        if let leftColor = leftColor, let rightColor = rightColor {
            gradientLayer.colors = [leftColor.cgColor, rightColor.cgColor]
            gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        }
        
        layer.insertSublayer(gradientLayer, at: 0)
        self.clipsToBounds = true // 确保按钮边界内的子图层能够被剪切
        
        var type: BorderType = .none
        if let borderColor = borderColor, borderWidth != 0 {
            type = .solid(width: borderWidth, color: borderColor)
        }
        bezierCorner(corners: corners, cornerRadii: cornerRadii, border: type)
    }

}
